<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Admin;

use think\Request;

/**
 * 用户通知后台控制器
 */
class NotificationController extends BaseController
{
    /**
     * 默认入口方法（兼容资源路由）
     */
    public function index()
    {
        return $this->getList(request());
    }
    
    /**
     * 获取通知列表
     */
    public function getList(Request $request)
    {
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        $userId = $request->param('user_id/d', 0);
        $type = $request->param('type/d', 0);
        $isRead = $request->param('is_read', '');
        $title = $request->param('title', '');
        $startTime = $request->param('start_time', '');
        $endTime = $request->param('end_time', '');
        
        $params = [];
        if ($userId > 0) {
            $params['user_id'] = $userId;
        }
        if ($type > 0) {
            $params['type'] = $type;
        }
        if ($isRead !== '') {
            $params['is_read'] = $isRead;
        }
        if (!empty($title)) {
            $params['title'] = $title;
        }
        if (!empty($startTime)) {
            $params['start_time'] = $startTime;
        }
        if (!empty($endTime)) {
            $params['end_time'] = $endTime;
        }
        
        $result = $this->notificationModel->getList($params, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取通知详情
     */
    public function getDetail(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $detail = $this->notificationModel->getDetail($id);
        if (!$detail) {
            return $this->error('通知不存在');
        }
        
        return $this->success('获取成功', $detail);
    }
    
    /**
     * 发送通知给单个用户
     */
    public function sendToUser(Request $request)
    {
        $userId = $request->param('user_id/d', 0);
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $type = $request->param('type/d', 1);
        $url = $request->param('url', '');
        
        if ($userId <= 0) {
            return $this->error('用户ID不能为空');
        }
        
        if (empty($title)) {
            return $this->error('标题不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $data = [
            'title' => $title,
            'content' => $content,
            'type' => $type,
            'url' => $url,
        ];
        
        $result = $this->notificationModel->sendToUser($userId, $data);
        if (!$result) {
            return $this->error('发送失败');
        }
        
        return $this->success('发送成功', ['id' => $result]);
    }
    
    /**
     * 发送通知给多个用户
     */
    public function sendToUsers(Request $request)
    {
        $userIds = $request->param('user_ids');
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $type = $request->param('type/d', 1);
        $url = $request->param('url', '');
        
        if (empty($userIds)) {
            return $this->error('用户ID不能为空');
        }
        
        if (!is_array($userIds)) {
            $userIds = explode(',', $userIds);
        }
        
        if (empty($title)) {
            return $this->error('标题不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $data = [
            'title' => $title,
            'content' => $content,
            'type' => $type,
            'url' => $url,
        ];
        
        $result = $this->notificationModel->sendToUsers($userIds, $data);
        if (!$result) {
            return $this->error('发送失败');
        }
        
        return $this->success('发送成功', ['count' => count($result)]);
    }
    
    /**
     * 发送全局通知
     */
    public function sendGlobal(Request $request)
    {
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $type = $request->param('type/d', 1);
        $url = $request->param('url', '');
        
        if (empty($title)) {
            return $this->error('标题不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容不能为空');
        }
        
        $data = [
            'title' => $title,
            'content' => $content,
            'type' => $type,
            'url' => $url,
        ];
        
        $result = $this->notificationModel->sendGlobal($data);
        if (!$result) {
            return $this->error('发送失败');
        }
        
        return $this->success('发送成功', ['id' => $result]);
    }
    
    /**
     * 删除通知
     */
    public function delete(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->notificationModel->adminDelete($id);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 批量删除通知
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        $result = $this->notificationModel->adminBatchDelete($ids);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
}
